package com.gopay.bis.bocom.ebank.operation;

import com.bankcomm.netpay.Config;
import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.bocom.ebank.pay.BocomB2cMessage;
import com.gopay.common.domain.bank.bocom.ebank.pay.b2b.req.BocomB2bReq;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.ICertHelper;

public class BocomB2bCertHelper implements ICertHelper {
	
	private String filePath;

    @Override
    public String sign(BaseMessage message) throws GopayException {
    	try {
            // B2B
    		BocomB2bReq messageB2b = (BocomB2bReq) message;
            BankLogUtils.printB2BSignPlainLog(messageB2b);
            String configID = "b2bpay";
            Config.loadConfig(filePath);
            String signMsg = Config.detachedSign(configID,messageB2b.getPlainMessage());
            BankLogUtils.printB2BSignSignatureLog(messageB2b, signMsg);
            return signMsg;
        } catch (Exception e) {
            BankLogUtils.printB2BSignExceptionLog(message, e);
            throw new GopayException(ProcCodeConstants.PROC_CODE_100E6028);
        }
    }
    

    @Override
    public Object verify(BaseMessage message) throws GopayException {
    	try {
    		BocomB2bReq bocomB2bMessage = (BocomB2bReq) message;
            boolean flag = true;
            
            String configID = "b2bpay";           
            Config.loadConfig(filePath);
            String verifyRes = Config.detachedVerify(configID,bocomB2bMessage.getPlainMessage(), bocomB2bMessage.getSignMessage());
            if("".equals(verifyRes)) {
                flag = false;
            }            
            BankLogUtils.printB2BVerifyPlainLog(bocomB2bMessage);
            BankLogUtils.printB2BVerifySignatureLog(bocomB2bMessage);        
            BankLogUtils.printB2BVerifyResultLog(bocomB2bMessage, flag);
            return flag;
        } catch (Exception e) {
            BankLogUtils.printB2BSignExceptionLog(message, e);
            throw new GopayException(ProcCodeConstants.PROC_CODE_100E6030);
        }    
    }

	public String getFilePath() {
		return filePath;
	}


	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}}
